package com.kevis.zhunblog.admin.customHtmlTag;

import com.kevis.zhunblog.admin.security.entity.SecurityUser;
import com.kevis.zhunblog.core.entities.PowerMenuEntity;
import com.kevis.zhunblog.mapper.PowerMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Component
public class HtmlHelper {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private PowerMenuMapper powerMenuMapper;

    public String linkJavaScript(String... actionName) {
        return toAction(1, "", "", actionName);
    }

    public String actionGuid(String... actionName) {
        return toAction(2, "", "", actionName);
    }

    public String actionUrl(String... actionName) {
        return toAction(4, "", "", actionName);
    }

    private String toAction(int type, String isPlain, String dataOptions, String[] actionNames) {
        final String isPlainText = isPlain;
        final String dataOptionsText = dataOptions;
        StringBuilder html = new StringBuilder();
        String actionGuid = request.getParameter("actionguid");
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            SecurityUser userDetails = (SecurityUser) principal;
            //String name = userDetails.getUsername();
            int roleId = userDetails.getRoleId();
            List<String> strings = Arrays.asList(actionNames);
            List<PowerMenuEntity> powerMenuEntityList = null;
            if (roleId == 9999) {
                //powerMenuEntityList = powerMenuMapper.getListByMenuCode(actionGuid, strings);
                powerMenuEntityList = powerMenuMapper.getListByMenuCode(actionGuid, actionNames);
            } else {
                powerMenuEntityList = powerMenuMapper.getListByMenuCodeAndRoleId(actionGuid, strings, roleId);
            }
            if (powerMenuEntityList != null && powerMenuEntityList.size() > 0) {
                powerMenuEntityList.forEach(t -> {
                    switch (type) {
                        case 0:
                            html.append("<a");
                            html.append(" href=\"javascript:");
                            html.append(t.getMenuCode());
                            html.append("('");
                            html.append(t.getGuid());
                            html.append("',rowid)");
                            html.append(";\">");
                            html.append("[ ");
                            html.append(t.getTitle());
                            html.append(" ]");
                            html.append("</a>");
                            break;
                        case 1:
                            html.append(t.getMenuCode());
                            html.append("('");
                            html.append(t.getGuid());
                            html.append("', rowid);");
                            break;
                        case 2:
                            html.append(t.getGuid());
                            break;
                        case 3:
                            //有这个按钮权限，开始构建按钮文本
                            html.append("<a");
                            html.append(" href=\"javascript:void(0);\"");
                            html.append(" class=\"easyui-linkbutton\"");
                            html.append(String.format(" data-options=\"iconCls:'%s',plain:%s%s\" "
                                    , t.getIcon(), isPlainText, dataOptionsText));
                            html.append(String.format(" onclick=\"%s('%s')\" >", t.getMenuCode(), t.getGuid()));
                            html.append(t.getTitle());
                            html.append("</a>");
                            break;
                        case 4:
                            html.append("/zhun/post.do?actionguid=" + t.getGuid());
                    }
                });
            }
        }
        return html.toString();
    }
}
